package org.index.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.ajax.common.AjaxMap;
import org.index.service.AdService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;

@RestController
@CrossOrigin
public class AdController {

	@Reference(version = "1.0.0", application = "${dubbo.application.id}", url = "dubbo://localhost:12346")
	private AdService<?> adService;

	private HttpServletRequest req;
	
	
	public AdController(HttpServletRequest req) {
		// TODO Auto-generated constructor stub
		this.req = req;

	}

	@RequestMapping("/getAd")
	public Map<String, ?> getAd() {
		
		List<?> adLIst = this.adService.getAdList();
		
		return AjaxMap.getInstance(adLIst).getAjaxResultByMap();
	}
	
	@RequestMapping("/getBanner")
	public Map<String, ?> getBanner() {
		
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<String, String>();

		Enumeration<String> param = this.req.getParameterNames();
		while (param.hasMoreElements()) {
			String paramName = param.nextElement();

			String paramValue = this.req.getParameter(paramName);
			// 形成键值对应的map
			map.put(paramName, paramValue);
		}
		
		return AjaxMap.getInstance(this.adService.getBanner(map)).getAjaxResultByMap();
	}
	
	@RequestMapping("/getS")
	public String getS()
	{
		return this.adService.getRes().toString();
	}
	
}
